<?php
if (COUNT($_POST))
{
	$isgetor = $this->get('getor');
	$credit = $this->get('credit');
	$iid = $this->get('iid');
	$total_sales = $this->get('sales');
	$quantity = $this->get('qty');
	$amt = $this->get('amt');
	$dscnt = $this->get('dscnt');
	$orn = $this->get('orn');
	$iscid = $this->get('iscid');
	$name = $this->get('name');
	$per = $this->get('per');
	$aid = $this->get('aid');
	
    Connection::getConnect();
	if ($isgetor)
	{
    	$sql = "SELECT MAX(orn) FROM item_sales";
    	Connection::setQuery($sql);
    	$torn = Connection::fetch();
		if ($credit)
		{
        	$sql = "INSERT INTO item_sales_credit(name, date, aid) VALUES('%s', NOW(), %d)";
        	Connection::setQueryf($sql, $name, $aid);
			$credit = mysql_insert_id();
		} else
			$credit = 0;
    	if (isset($torn[0]))
    		echo ($torn[0]+1) . ":" . $credit;
    	else
    		echo 1 . ":" . $credit;
		exit;
	}
	
	$sql = "INSERT INTO item_sales(orn, iid, total_sales, quantity, date, aid, amt_paid, discount, iscid) VALUES(%d, %d, %d,  %d, NOW(), %d, %f,  %f, %d)";
	Connection::setQueryf($sql, $orn, $iid, $total_sales, $quantity, $login->aid, $amt, $dscnt, $iscid);
	if ($iscid != 0)
	{
		$sql = "UPDATE item_sales_credit SET balance=(SELECT SUM(total_sales)*(100-discount)/100 FROM item_sales i WHERE iscid=%d), total=balance WHERE iscid=%d";
		Connection::setQueryf($sql, $iscid, $iscid);
	}
	$sql = "UPDATE item_inventory SET quantity=quantity-(%d*%d) WHERE iid=%d";
	Connection::setQueryf($sql, $quantity, $per, $iid);
}
 ?>